/*
 * @Author: ls
 * @Date: 2021-11-19 16:47:44
 * @LastEditTime: 2021-11-19 17:05:34
 * @LastEditors: ls
 * @Description: 事件
 * @FilePath: \BezierCurvePathCreater-master\assets\scripts\EventListener.js
 */

var EventListener = (function () {
	let _this = {};

	/**句柄 */
	var handlers = {};

	/**
	 * 设置监听事件
	 * @param {*} eventType 类型
	 * @param {*} method 方法
	 */
	_this.on = function (eventType, method) {
		handlers[eventType] = handlers[eventType] || [];
		handlers[eventType].push(method);
	};

	/**
	 * 触发事件
	 * @param {*} eventType 类型
	 * @param {*} data 事件
	 * @returns
	 */
	_this.emit = function (eventType, data) {
		if (!handlers[eventType]) {
			return;
		}
		for (var i = 0, len = handlers[eventType].length; i < len; i++) {
			handlers[eventType][i].call(null, data);
		}
	};

	/**
	 * 删除监听事件
	 * @param {*} eventType 类型
	 * @param {*} method 方法
	 */
	_this.off = function (eventType, method) {
		let handler = handlers[eventType];
		for (var i = 0, len = handler.length; i < len; i++) {
			if (handler[i] === method) {
				handler.splice(i, 1);
			}
		}
	};

	return _this;
})();

module.exports = EventListener;
